﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.MonHocService;

namespace eTMS.Forms.Reports
{
    public partial class FrmBangKeDanhSachNghiHoc : eTMS.Controls.BaseForm
    {
        public FrmBangKeDanhSachNghiHoc()
        {
            InitializeComponent();
        }
        ChuongTrinhDaoTaoController ctrlCTDT;
        DiemDanhSinhVienController controller;

        private void FrmBangKeDanhSachNghiHoc_Load(object sender, EventArgs e)
        {
            if (controller == null)
            {
                controller = new DiemDanhSinhVienController();
            }
            ctrlCTDT = new ChuongTrinhDaoTaoController();
            NamHocController.LoadAllNamHoc(false);
            HocKyController.LoadAllHocKy(false);
            comboBoxNamHoc1.BindData();
            comboBoxHocKy1.BindData();
        }
        bool isInternal = false;
        private void BindMonHoc()
        {
            if (comboBoxHocKy1.SelectedIndex < 0) return;
            if (comboBoxNamHoc1.SelectedIndex < 0) return;
            isInternal = true;
            List<MonHocEntity> ls = ctrlCTDT.LoadAllMonHocHocTheoNganhHocKhoaHoc(treeKhoaNganhKhoaHoc1.KhoaHoc_ID, treeKhoaNganhKhoaHoc1.CurrentNganhHoc_ID);
            txtMaMH.DisplayMember = "MaMonHoc";
            txtMaMH.ValueMember = "MonHoc_ID";
            txtTenMH.DisplayMember = "TenMonHoc";
            txtTenMH.ValueMember = "MonHoc_ID";
            txtMaMH.DataSource = ls;
            txtTenMH.DataSource = ls;
            isInternal = false;
        }


        private int currentNganhHoc = 0;
        private void treeKhoaNganhKhoaHoc1_OnChangeTreeSelected(object sender, EventArgs e)
        {
            if (treeKhoaNganhKhoaHoc1.SelectedLop_ID > 0 && treeKhoaNganhKhoaHoc1.CurrentNganhHoc_ID > 0)
            {
                if (treeKhoaNganhKhoaHoc1.CurrentNganhHoc_ID != currentNganhHoc)
                {
                    BindMonHoc();
                    currentNganhHoc = treeKhoaNganhKhoaHoc1.CurrentNganhHoc_ID;
                }
            }
        }

        private void treeKhoaNganhKhoaHoc1_OnChangeKhoaHoc(object sender, EventArgs e)
        {
            currentNganhHoc = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private string GetFilter()
        {
            string filter = "";
            if (txtMaSV.Text.Trim().Length > 0)
            {
                filter += "AND MaSinhVien='" + txtMaSV.Text.Trim() + "'";
            }
            if (txtTenSV.Text.Trim().Length > 0)
            {
                filter += "AND TenSinhVien LIKE '%" + txtTenSV.Text.Trim() + "%'";
            }
            if (filter.Length > 0)
                filter = filter.Substring(4);
            return filter;
        }
        private void btnFind_Click(object sender, EventArgs e)
        {
            SinhVienController sinhVienCtrl = new SinhVienController();
            controller.LoadAllDSSinhVienNghiHoc((int)comboBoxHocKy1.SelectedValue, (int)comboBoxNamHoc1.SelectedValue, (int)txtMaMH.SelectedValue, GetFilter());
            htmlReportViewer1.ReportDocument.Parameters.Clear();
            List<int> lsID = controller.GetListSinhVien_Id();
            if (lsID.Count > 0)
            {
                sinhVienCtrl.LoadSinhVienByListID(lsID);
                controller.AddSinhVienInfoToTable(sinhVienCtrl.Table);
                
            }
            SystemControllers.ReportHelper.AddHtmlReportParams(htmlReportViewer1.ReportDocument.Parameters);
            htmlReportViewer1.LoadReport(SystemControllers.ReportHelper.GetReportPath("19_DanhSachNghiHocTheoMon.htm"));
            DataSet ds=new DataSet();
            ds.Tables.Add(controller.Table);
            htmlReportViewer1.ReportDocument.DataSource = ds;
            htmlReportViewer1.ShowReport();
            //sinhVienCtrl.Table = null;
        }

        private void comboBoxHocKy1_SelectedValueChanged(object sender, EventArgs e)
        {
            BindMonHoc();
        }


    }
}
